import numpy as np

from agent import Agent
from wall import Wall


class VerticalModel:
    diw: float
    flg: bool
    niw: np.ndarray
    foot: np.ndarray


def get_distance_a_to_w(agent: Agent, wall: Wall):
    v = VerticalModel()
    v.foot = foot_point_of_point_to_segment(agent.position, wall)
    v.flg = dot_on_line(v.foot, wall)
    v.diw = np.linalg.norm(agent.position - v.foot)
    v.niw = (agent.position - v.foot) / v.diw
    return v


def foot_point_of_point_to_segment(point: np.ndarray, wall: Wall):
    x0, y0 = point[0], point[1]
    x1, y1 = wall.end_point[0], wall.end_point[1]
    x2, y2 = wall.start_point[0], wall.start_point[1]

    k = -((x1 - x0) * (x2 - x1) + (y1 - y0) * (y2 - y1)) / ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))
    xf = k * (x2 - x1) + x1
    yf = k * (y2 - y1) + y1
    return np.array([xf, yf])


def dot_on_line(point: np.ndarray, wall: Wall):
    return np.dot(point - np.array(wall.end_point), point - np.array(wall.start_point)) <= 0
